//
//  BanJiViewList.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/15.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "BanJiViewList.h"
#import "SYRequestAPI+Teach.h"
#import "CJClassSelectedView.h"

@implementation BanJiViewList

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        [self initSubView];
    }
    return self;
}
- (LWQSwichButtonView *)navView{
    if (!_navView) {
        NSMutableArray *array = [NSMutableArray new];
        [array addObject:@"班级学生"];
        [array addObject:@"班级考勤"];
        [array addObject:@"班级课时"];
        LWQSwichButtonView *view = [[LWQSwichButtonView alloc]initWithFrame:CGRectMake(0, 0,MAINW-100, 44) andArray:array];
        view.delegate = self;
        _navView = view;
    }
    return _navView;
}
- (void)back{
    [UIView animateWithDuration:0.5 animations:^{
        CGRect rect = self.frame;
        rect.origin.x = APP_WIDTH-200;
        self.frame = rect;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [[NSNotificationCenter defaultCenter]removeObserver:self name:@"TeachChangeNotice" object:nil];
    }];
    [self sendShowView];
}
- (void)sendShowView{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"BanJiShowView" object:nil];
}
- (void)scrollViewChangeButtom:(int)index{
    for (int i =0; i<_viewArray.count; i++) {
        UIView *view = _viewArray[i];
        if (i == index) {
            view.hidden = NO;
        }else{
            view.hidden = YES;
        }
    }
//    currentIndex = index;
//    self.mainScrollView.contentOffset = CGPointMake(_mainw*index, 0);
//
    if (index == 0) {
        if (_banJiXueShengView.haveLoad == NO) {
            _banJiXueShengView.haveLoad = YES;
            [_banJiXueShengView getBanJiXueShengRequest];
        }
        
    }else if (index == 1){
        if (_banJiKaoQingView.haveLoad == NO) {
            _banJiKaoQingView.haveLoad = YES;
            _banJiKaoQingView.classID = self.classID;
            [_banJiKaoQingView getBanJiKaoQingRequest];
        }
    }else{
        if (_banJiKeShiView.haveLoad == NO) {
            _banJiKeShiView.haveLoad = YES;
            _banJiKeShiView.classID = self.classID;
            [_banJiKeShiView getBanJiKeShiRequest];
        }
    }
}
- (void)initSubView{
    _viewArray = [NSMutableArray new];
//    [self addSubview:self.mainScrollView];
    navBackView = [[UIView alloc]init];
    navBackView.backgroundColor = MAINBACKCOLOR;
    [self addSubview:navBackView];
    [navBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.height.equalTo(@44);
    }];
    [navBackView addSubview:self.navView];
    //班级详情不需要选择班级
//    [navBackView addSubview:self.banJiButton];
//    [_banJiButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.equalTo(@80);
//        make.right.equalTo(@0).offset(-20);
//        make.top.equalTo(@0);
//        make.height.equalTo(@44);
//    }];
    [self addView];
#pragma mark - 目前每次都要使用
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeView:) name:@"ChangeCellSpaNotice" object:nil];
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeView:) name:@"TeachChangeNotice" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(addChildrenView:) name:@"BanJiAddNextViewNotice" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(addXueShengChildrenView:) name:@"XueShengXiangQingShowNotice" object:nil];
}
- (void)reloadData{
    [self getTopDetailRequest];
    _banJiXueShengView.classID = self.classID;
    [_banJiXueShengView getBanJiXueShengRequest];
    if (_banJiKaoQingView.haveLoad == YES) {
        [_banJiKaoQingView getBanJiKaoQingRequest];
    }
    if (_banJiKeShiView.haveLoad == YES) {
        _banJiKeShiView.classID = self.classID;
        [_banJiKeShiView getBanJiKeShiRequest];
    }
}
#pragma mark - 添加学生详情
-(void)addXueShengChildrenView:(NSNotification *)info{
    [MBProgressHUD showHUDAddedTo:self animated:YES];
    int status = [[info.userInfo objectForKey:@"status"] intValue];
    if (status == 0) {
        [UIView animateWithDuration:0.5 animations:^{
            self->_detailVC.view.frame = CGRectMake(self.mainw, 0, self.mainw, APP_HEIGHT-84);
        } completion:^(BOOL finished) {
            [self->_detailVC.view removeFromSuperview];
            self->_detailVC = nil;
            [MBProgressHUD hideHUDForView:self animated:YES];
        }];
    }else{
        _detailVC = [[XueShengXiangQingVC alloc]init];
        _detailVC.view.backgroundColor = [UIColor whiteColor];
        [self addSubview:_detailVC.view];
        [_detailVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.right.equalTo(@0);
            make.top.equalTo(@0);
            make.bottom.equalTo(@0);
        }];
//        [UIView animateWithDuration:0.5 animations:^{
//            self->_detailVC.view.frame = CGRectMake(0, 0, self.mainw, APP_HEIGHT-84);
//        }];
        [MBProgressHUD hideHUDForView:self animated:YES];
    }
    
}
#pragma mark - 添加子视图
- (void)addView{
    [self addSubview:self.banJiXueShengView];
    [_banJiXueShengView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@44);
        make.bottom.equalTo(@0);
    }];
    [_banJiXueShengView.tableList reloadData];
    [_viewArray addObject:_banJiXueShengView];
    
    
    [self addSubview:self.banJiKaoQingView];
    [_banJiKaoQingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@44);
        make.bottom.equalTo(@0);
    }];
    _banJiKaoQingView.hidden = YES;
    [_banJiKaoQingView.tableList reloadData];
    [_viewArray addObject:_banJiKaoQingView];
    
    [self addSubview:self.banJiKeShiView];
    [_banJiKeShiView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@44);
        make.bottom.equalTo(@0);
    }];
    _banJiKeShiView.hidden = YES;
    [_banJiKeShiView.mainCollectionView reloadData];
    [_viewArray addObject:_banJiKeShiView];
}
//#pragma mark - 这个基本每个类都要用
//- (void)changeView:(NSNotification *)info{
//    [_banJiXueShengView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(@0);
//        make.width.equalTo(self.mainScrollView.mas_width);
//        make.top.equalTo(@0);
//        make.bottom.equalTo(@0);
//    }];
//    [_banJiKaoQingView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self->_banJiXueShengView.mas_right);
//        make.width.equalTo(self.mainScrollView.mas_width);
//        make.top.equalTo(@0);
//        make.bottom.equalTo(@0);
//    }];
//    [_banJiKeShiView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self->_banJiKaoQingView.mas_right);
//        make.width.equalTo(self.mainScrollView.mas_width);
//        make.top.equalTo(@0);
//        make.bottom.equalTo(@0);
//    }];
////    int type = [[info.userInfo objectForKey:@"Type"] intValue];
////    _mainw = [PublickFunctionObject currentScreenSize:type];
////
////    self.frame = CGRectMake(0, 0, _mainw, APP_HEIGHT-84);
////
////    NSLog(@"view宽度 %f",_mainw);
////    _mainScrollView.frame = CGRectMake(0,44, self.mainw, APP_HEIGHT-84);
////    _mainScrollView.contentSize = CGSizeMake(_mainw*3, 0);
////    self.mainScrollView.contentOffset = CGPointMake(_mainw*currentIndex, 0);
////
////    navBackView.frame = CGRectMake(0, 0, _mainw, 44);
////    _navView.frame = CGRectMake(0, 0,_mainw-100, 44);
////    [_navView uploadLayout];
////    _banJiButton.frame = CGRectMake(_mainw-90, 12, 69, 20);
////    //班级学生刷新
////    _banJiXueShengView.frame = CGRectMake(0, 0, _mainw, self.frame.size.height);
////    [_banJiXueShengView.tableList reloadData];
////    //班级考勤刷新
////    _banJiKaoQingView.frame = CGRectMake(_mainw,0, _mainw, self.frame.size.height);
////    [_banJiKaoQingView.tableList reloadData];
////    //班级课时刷新
////    _banJiKeShiView.frame = CGRectMake(_mainw*2,0, _mainw, self.frame.size.height);
////    [_banJiKeShiView.mainCollectionView reloadData];
////    //班级学生详情
////    _detailVC.view.frame = CGRectMake(0, 0, self.mainw, APP_HEIGHT);
//    //班级考勤详情
////    _banJiKaoQingNextView.frame = CGRectMake(0,0,self.mainw, APP_HEIGHT);
////    _banJiKaoQingNextView.mainw = self.mainw;
////    [_banJiKaoQingNextView.tableList reloadData];
//
//}

//- (UIScrollView *)mainScrollView{
//    if (!_mainScrollView) {
//        UIScrollView *view = [[UIScrollView alloc]init];
//        view.frame = CGRectMake(0, 44, MAINW, APP_HEIGHT-84);
//        view.contentSize = CGSizeMake(MAINW*3, 0);
//        view.showsHorizontalScrollIndicator = YES;
//        view.showsVerticalScrollIndicator = NO;
//        view.pagingEnabled = YES;
//        view.scrollEnabled = NO;
//        view.backgroundColor = MAINBACKCOLOR;
//        _mainScrollView = view;
//    }
//    return _mainScrollView;
//}
- (MinButtonViewBtn *)banJiButton{
    if (!_banJiButton) {
        MinButtonViewBtn *btn = [[MinButtonViewBtn alloc]initWithFrame:CGRectMake(_mainw-90, 12, 69, 20) andType:ButtonImageInRight];
        btn.bottomLabel.text = @"选择班级";
        btn.bottomLabel.font = [UIFont systemFontOfSize:12];
        btn.bottomLabel.textColor = TITLECOLOR;
        btn.topImage.image = [UIImage imageNamed:@"arrow_down"];
        [btn addTarget:self action:@selector(nianjiButton:) forControlEvents:UIControlEventTouchUpInside];
        [btn.bottomLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.right.equalTo(@0).offset(-15);
        }];
        _banJiButton = btn;
    }
    return _banJiButton;
}
- (void)nianjiButton:(MinButtonViewBtn *)sender{
    //选择班级
    __weak typeof(self) mySelf = self;
    [CJClassSelectedView showDatePickerWithTitle:@"选择班级" minDateStr:@"11" resultBlock:^(TeacherClassListModel * _Nonnull selectValue) {
      //  NSLog(@"输出选择的班级 %@",selectValue);
        self->mainClassModel = selectValue;
        if (mySelf.classID == self->mainClassModel.id) {
            
        }else{
            //倘若切换了班级，在切换按钮的时候界面从新刷新
            [mySelf.banJiXueShengView reloadIndex];
            [mySelf.banJiKaoQingView reloadIndex];
            [mySelf.banJiKeShiView reloadIndex];
            //此页面的三个元素都要更新
            [mySelf reloadData];
        }
        mySelf.classID = self->mainClassModel.id;
        [mySelf getTopDetailRequest];
    }];
}
- (BanJiXueShengView *)banJiXueShengView{
    if (!_banJiXueShengView) {
        BanJiXueShengView *view = [[BanJiXueShengView alloc]initWithFrame:CGRectMake(0, 0, _mainw, APP_HEIGHT-84)];
        view.backgroundColor = MAINBACKCOLOR;
        view.mainDelegate = self;
        _banJiXueShengView = view;
    }
    return _banJiXueShengView;
}
- (void)addBanJiXueShengView:(BanJiStudentListModel *)model{
    [MBProgressHUD showHUDAddedTo:self animated:YES];
    _detailVC = [[XueShengXiangQingVC alloc]initWothStudentID:model.studentId];
    _detailVC.view.backgroundColor = [UIColor whiteColor];
    _detailVC.view.frame = CGRectMake(self.mainw, 0, self.mainw, APP_HEIGHT);
    [self addSubview:_detailVC.view];
    [_detailVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
//    [UIView animateWithDuration:0.5 animations:^{
//        self->_detailVC.view.frame = CGRectMake(0, 0, self.mainw, APP_HEIGHT);
//    }];
    [MBProgressHUD hideHUDForView:self animated:YES];
}

- (BanJiKaoQingView *)banJiKaoQingView{
    if (!_banJiKaoQingView) {
        BanJiKaoQingView *view = [[BanJiKaoQingView alloc]initWithFrame:CGRectMake(self.mainw, 0, self.mainw, APP_HEIGHT-84)];
        view.backgroundColor = MAINBACKCOLOR;
        _banJiKaoQingView = view;
    }
    return _banJiKaoQingView;
}
- (BanJiKeShiView *)banJiKeShiView{
    if (!_banJiKeShiView) {
        BanJiKeShiView *view = [[BanJiKeShiView alloc]initWithFrame:CGRectMake(self.mainw*2, 0, _mainw, APP_HEIGHT-84)];
        view.backgroundColor = MAINBACKCOLOR;
        view.mainDelegate = self;
        _banJiKeShiView = view;
    }
    return _banJiKeShiView;
}
#pragma mark - 添加班级课时考勤详情
- (void)addKeShiKaoQing:(BanJiKeShiModel *)keShiModel{
    [MBProgressHUD mj_showMessage:nil toView:self];
    _kaoQingDetailView = [[KeShiKaoQingView alloc]initWithFrame:CGRectMake(self.frame.size.width,0, self.frame.size.width, APP_HEIGHT) andModel:keShiModel andTopModel:_mainModel];
    [self addSubview:_kaoQingDetailView];
    [_kaoQingDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    [MBProgressHUD mj_hideHUDForView:self];
//    [UIView animateWithDuration:0.5 animations:^{
//        self->_kaoQingDetailView.frame = CGRectMake(0,0, self.frame.size.width, APP_HEIGHT);
//    }];
}
- (void)addKeShiXiangQing:(BanJiKeShiModel *)keShiModel{
    _kaoQingDetailView = [[KeShiKaoQingView alloc]initWithFrame:CGRectMake(self.frame.size.width,0, self.frame.size.width, APP_HEIGHT) andModel:keShiModel andTopModel:_mainModel];
    [self addSubview:_kaoQingDetailView];
    [_kaoQingDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];

}
- (void)addChildrenView:(NSNotification *)info{
    int index = [[info.userInfo objectForKey:@"index"] intValue];
    int status = [[info.userInfo objectForKey:@"status"] intValue];
    if (status == 0) {
        [UIView animateWithDuration:0.5 animations:^{
            self->_banJiKaoQingNextView.frame = CGRectMake(self->_mainw,0, self->_mainw, APP_HEIGHT);
        } completion:^(BOOL finished) {
            [self->_banJiKaoQingNextView removeFromSuperview];
        }];
    }else{
        //添加View
        [self addBanJiNextButton:info];
    }
   
}
#pragma mark -班级考勤下级页面
- (void)addBanJiNextButton:(NSNotification *)info{
    _banJiKaoQingNextView = [[BanJiKaoQingNextView alloc]initWithFrame:CGRectMake(self.frame.size.width,0, self.frame.size.width, APP_HEIGHT)];
    _banJiKaoQingNextView.mainw = self.mainw;
    BanJiKaoQingListModel *model = [info.userInfo objectForKey:@"Model"];
    
    _banJiKaoQingNextView.mainModel = _mainModel;
    _banJiKaoQingNextView.courseName = self.courseName;
    _banJiKaoQingNextView.studentModel = model;
    [_banJiKaoQingNextView reloadData];
    [self addSubview:_banJiKaoQingNextView];
    [_banJiKaoQingNextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.top.equalTo(self.mas_top);
        make.height.equalTo(@0).offset(APP_HEIGHT);
    }];
//    [UIView animateWithDuration:0.5 animations:^{
//        [self->_banJiKaoQingNextView mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.mas_left);
//            make.right.equalTo(self.mas_right);
//        }];
//         [self layoutIfNeeded];
//    }];
}
#pragma mark - 教学-班级-未结业-顶部信息
- (void)getTopDetailRequest{
    NSDictionary *param = @{@"classId":_classID};
    [MBProgressHUD mj_hideHUDForView:self];
    [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI sy_getClassServiceClassDetail:param success:^(id  _Nonnull responseObject) {
       // NSLog(@"教学-班级-未结业-顶部信息 %@",responseObject);
        NSDictionary *dic = [responseObject objectForKey:@"data"];
        self->_mainModel = [BanJiListTopDetailModel yy_modelWithJSON:dic];
        [self updateView:self->_mainModel];
        [MBProgressHUD mj_hideHUDForView:self];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
    }];
}
- (void)updateView:(BanJiListTopDetailModel *)model{
    _banJiXueShengView.mainModel = model;
    _banJiXueShengView.courseName = self.courseName;
    _banJiXueShengView.renKeLaoShiName = self.renKeLaoShiName;
    _banJiXueShengView.students = self.students;
    [_banJiXueShengView updateModel];
    
    _banJiKaoQingView.mainModel = model;
    _banJiKaoQingView.courseName = self.courseName;
    _banJiKaoQingView.renKeLaoShiName = self.renKeLaoShiName;
    _banJiKaoQingView.students = self.students;
    _banJiKaoQingView.classID = self.classID;
    [_banJiKaoQingView updateModel];
    
    _banJiKeShiView.mainModel = model;
    _banJiKeShiView.courseName = self.courseName;
    _banJiKeShiView.renKeLaoShiName = self.renKeLaoShiName;
    _banJiKeShiView.students = self.students;
    [_banJiKeShiView updateModel];
}
@end
